//
//  DLinkList.cpp
//  DataStructure
//
//  Created by 劢克科技 on 2020/4/7.
//  Copyright © 2020 Shuleiming. All rights reserved.
//

#include "DLinkList.hpp"

// 初始化
bool InitDLinkList(DLinkList &L)
{
    L = (DLinkList)malloc(sizeof(DLinkList));
    if (L==NULL)
        return false;
    L->prior = NULL;
    L->next = NULL;
    return true;
}

// 判断空
bool IsEmpty(DLinkList L)
{
    return L->next == NULL;
}

// 插入
bool InsertNextNode(DNode *p, DNode *s)
{
    if (p == NULL || s == NULL)
        return false;
    s->next = p->next;
    if (p->next != NULL) {
        p->next->prior = s;
    }
    s->prior = p;
    p->next = s;
    return true;
}

// 删除
bool DeleteNextNode(DNode *p)
{
    if (p == NULL || p->next == NULL)
        return false;
    DNode *q = p->next;
    p->next = q->next;
    if (q->next != NULL) {
        q->next->prior = p;
    }
    free(q);
    return true;
}

// 销毁
void DestoryList(DLinkList &L)
{
    while (L->next != NULL) {
        DeleteNextNode(L);
    }
    free(L);
    L = NULL;
}
